package com.kekezu.kppw.data;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.tsz.afinal.FinalDb;

import org.apache.http.NameValuePair;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import com.kekezu.kppw.bean.UserBean;
import com.kekezu.kppw.utils.ConfigInc;
import com.kekezu.kppw.utils.HttpUtils;

@SuppressWarnings("deprecation")
public class WorkData {
	static FinalDb db;
	static Intent intent;

	/**
	 * 购买作品
	 * 
	 * @param strId
	 * @param context
	 * @return
	 */
	public static Map<String, String> buyGoods(String strId, Context context) {
		Map<String, String> resMap = new HashMap<String, String>();
		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/buyGoods?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		Map<String, String> params = new HashMap<String, String>();
		params.put("token", users.get(0).getToken());
		params.put("goods_id", strId);

		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);
			String resCode = jsonObject.getString("code");
			String resMsg = jsonObject.getString("message");

			JSONObject jsondata = new JSONObject(jsonObject.getString("data"));
			// 对返回的数据进行判断
			if (resCode.equals("1000")) {
				resMap.put("code", resCode);
				resMap.put("order_id", jsondata.getString("order_id"));
				resMap.put("message", resMsg);
			} else {
				resMap.put("code", resCode);
				resMap.put("message", resMsg);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resMap;
	}

	/**
	 * 购买商品余额付款
	 * 
	 * @param orderId
	 *            订单id
	 * @param intType
	 *            支付类型
	 * @param strPwd
	 *            支付密码
	 * @param context
	 * @return
	 */
	public static Map<String, String> cashPayGoods(String orderId, int intType,
			String strPwd, Context context) {
		Map<String, String> resMap = new HashMap<String, String>();

		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/cashPayGoods";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("order_id", orderId));
		params.add(new BasicNameValuePair("pay_type", "" + intType));
		params.add(new BasicNameValuePair("password", strPwd));
		params.add(new BasicNameValuePair("token", users.get(0).getToken()));

		try {
			String strJson = httpUtils.executeRequest(urlString, params);
			JSONObject jsonObject = new JSONObject(strJson);
			String resCode = jsonObject.getString("code");
			String resMsg = jsonObject.getString("message");

			resMap.put("code", resCode);
			resMap.put("msg", resMsg);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return resMap;
	}

	/**
	 * 购买商品第三方支付
	 * 
	 * @param orderId
	 *            订单id
	 * @param strType
	 *            支付类型（微信，支付宝）
	 * @param context
	 * @return
	 */
	public static String ThirdCashGoodsPay(String orderId, String strType,
			Context context) {
		String strRes = null;
		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/ThirdCashGoodsPay?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		Map<String, String> params = new HashMap<String, String>();
		params.put("order_id", orderId);
		params.put("pay_type", strType);
		params.put("token", users.get(0).getToken().toString());

		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			Log.e("strJson", strJson);

			JSONObject jsonObject = new JSONObject(strJson);
			String resString = jsonObject.getString("code");

			// 对返回的数据进行判断
			if (resString.equals("1000")) {
				JSONObject jsondata = new JSONObject(
						jsonObject.getString("data"));

				if (strType.equals("alipay")) {
					strRes = jsondata.getString("payParam").toString();
				} else {
					strRes = jsondata.getString("params").toString();
				}

			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return strRes;
	}

	/**
	 * 购买商品的详情
	 * 
	 * @param strId
	 * @param context
	 * @return
	 */
	public static Map<String, String> buyGoodsDetail(String strId,
			Context context) {
		Map<String, String> resMap = new HashMap<String, String>();
		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/buyGoodsDetail?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		Map<String, String> params = new HashMap<String, String>();
		params.put("order_id", strId);
		params.put("token", users.get(0).getToken());
		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);
			String resCode = jsonObject.getString("code");
			String resMsg = jsonObject.getString("message");

			// 对返回的数据进行判断
			if (resCode.equals("1000")) {
				JSONObject jsondata = new JSONObject(
						jsonObject.getString("data"));

				resMap.put("id", jsondata.getString("id"));
				resMap.put("buy_uid", jsondata.getString("buy_uid"));
				resMap.put("cash", "￥ " + jsondata.getString("cash") + "/"
						+ jsondata.getString("unit"));
				resMap.put("status", jsondata.getString("status"));
				resMap.put("goods_id", jsondata.getString("goods_id"));
				resMap.put("title", jsondata.getString("title"));
				resMap.put("sales_num", jsondata.getString("sales_num"));
				resMap.put("uid", jsondata.getString("uid"));
				resMap.put("shop_id", jsondata.getString("shop_id"));
				resMap.put("address", jsondata.getString("address"));
				resMap.put("button_status", jsondata.getString("button_status"));
				resMap.put("attachment", jsondata.getString("attachment"));
				resMap.put("cover", jsondata.getString("cover"));
				resMap.put("username", jsondata.getString("username"));
			} else {
				Toast.makeText(context, resMsg, Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resMap;
	}

	/**
	 * 确认作品
	 * 
	 * @param strId
	 * @param context
	 * @return
	 */
	public static String confirmGoods(String strId, Context context) {
		String res = null;
		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/confirmGoods?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		Map<String, String> params = new HashMap<String, String>();
		params.put("order_id", strId);
		params.put("token", users.get(0).getToken());

		try {
			String strJson = httpUtils.httpGet(urlString, params, context);

			Log.e(strJson, "" + strJson);

			JSONObject jsonObject = new JSONObject(strJson);
			String resCode = jsonObject.getString("code");
			String resMsg = jsonObject.getString("message");
			// 对返回的数据进行判断
			if (resCode.equals("1000")) {
				res = resCode;
			} else {
				res = resMsg;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}

	/**
	 * 商品订单维权
	 * 
	 * @param strId
	 *            订单id
	 * @param intType
	 *            维权类型
	 * @param strDesc
	 *            维权详情
	 * @param context
	 * @return
	 */
	public static String rightGoods(String strId, int intType, String strDesc,
			Context context) {
		String res = null;
		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/rightGoods";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		MultipartEntity multipart = new MultipartEntity();
		try {
			multipart.addPart("token", new StringBody(users.get(0).getToken(),
					Charset.forName("UTF-8")));
			multipart.addPart("order_id",
					new StringBody(strId, Charset.forName("UTF-8")));
			multipart.addPart("type",
					new StringBody("" + intType, Charset.forName("UTF-8")));
			multipart.addPart("desc",
					new StringBody(strDesc, Charset.forName("UTF-8")));

			String strJson = httpUtils.httpPost(urlString, multipart);
			JSONObject jsonObject = new JSONObject(strJson);
			String resCode = jsonObject.getString("code");
			String resMsg = jsonObject.getString("message");
			// 对返回的数据进行判断
			if (resCode.equals("1000")) {
				res = resCode;
			} else {
				res = resMsg;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}

	public static String[] commentGoods(String strId, String intType,
			String strDesc, float speed, float quality, float attitude,
			Context context) {
		String res[] = new String[2];
		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/commentGoods";
		HttpUtils httpUtils = HttpUtils.getInstance();
		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("token", users.get(0).getToken()));
		params.add(new BasicNameValuePair("order_id", strId));
		params.add(new BasicNameValuePair("type", intType));
		params.add(new BasicNameValuePair("desc", strDesc));
		params.add(new BasicNameValuePair("speed_score", "" + speed));
		params.add(new BasicNameValuePair("quality_score", "" + quality));
		params.add(new BasicNameValuePair("attitude_score", "" + attitude));

		try {
			String strJson = httpUtils.executeRequest(urlString, params);
			JSONObject jsonObject = new JSONObject(strJson);

			res[0] = jsonObject.getString("code");
			res[1] = jsonObject.getString("message");

		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}

	public static Map<String, String> saleGoodsDetail(String strId,
			Context context) {
		Map<String, String> resMap = new HashMap<String, String>();
		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/saleGoodsDetail?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		Map<String, String> params = new HashMap<String, String>();
		params.put("order_id", strId);
		params.put("token", users.get(0).getToken());
		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);
			String resCode = jsonObject.getString("code");
			String resMsg = jsonObject.getString("message");

			// 对返回的数据进行判断
			if (resCode.equals("1000")) {
				JSONObject jsondata = new JSONObject(
						jsonObject.getString("data"));

				resMap.put("id", jsondata.getString("id"));
				resMap.put("buy_uid", jsondata.getString("buy_uid"));
				resMap.put("cash", "￥ " + jsondata.getString("cash") + "/"
						+ jsondata.getString("unit"));
				resMap.put("status", jsondata.getString("status"));
				resMap.put("goods_id", jsondata.getString("goods_id"));
				resMap.put("title", jsondata.getString("title"));
				resMap.put("sales_num", jsondata.getString("sales_num"));
				resMap.put("uid", jsondata.getString("uid"));
				resMap.put("shop_id", jsondata.getString("shop_id"));
				resMap.put("address", jsondata.getString("address"));
				resMap.put("cover", jsondata.getString("cover"));
				resMap.put("username", jsondata.getString("username"));
				resMap.put("avatar", jsondata.getString("avatar"));
				resMap.put("comment", jsondata.getString("comment"));

			} else {
				Toast.makeText(context, resMsg, Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resMap;
	}

	public static Map<String, String> getComment(String strId, Context context) {
		Map<String, String> resMap = new HashMap<String, String>();
		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/getComment?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		Map<String, String> params = new HashMap<String, String>();
		params.put("token", "" + users.get(0).getToken());
		params.put("order_id", strId);
		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);
			String resCode = jsonObject.getString("code");
			String resMsg = jsonObject.getString("message");

			Log.e("strJson", strJson);
			// 对返回的数据进行判断
			if (resCode.equals("1000")) {
				JSONObject jsondata = new JSONObject(
						jsonObject.getString("data"));

				resMap.put("id", jsondata.getString("id"));
				resMap.put("goods_id", jsondata.getString("goods_id"));
				resMap.put("uid", jsondata.getString("uid"));
				resMap.put("comment_by", jsondata.getString("comment_by"));
				resMap.put("speed_score", jsondata.getString("speed_score"));
				resMap.put("quality_score", jsondata.getString("quality_score"));
				resMap.put("attitude_score",
						jsondata.getString("attitude_score"));
				resMap.put("comment_desc", jsondata.getString("comment_desc"));
				resMap.put("type", jsondata.getString("type"));
				resMap.put("created_at", jsondata.getString("created_at"));
				resMap.put("name", jsondata.getString("name"));
				resMap.put("avatar", jsondata.getString("avatar"));
			} else {
				Toast.makeText(context, resMsg, Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resMap;
	}

}
